package com.atguigu.boot.admin.controller;

import com.atguigu.boot.admin.bean.Account;
import com.atguigu.boot.admin.bean.City;
import com.atguigu.boot.admin.bean.User;
import com.atguigu.boot.admin.service.AccountService;
import com.atguigu.boot.admin.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author : leimingchao
 * @date Date : 2021年11月19日 20:45
 */
@Controller
@Slf4j
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AccountService accountService;

    @Autowired
    private CityService cityService;

    // @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ResponseBody
    @PostMapping(value = "/city")
    public City saveCity(City city) {
        cityService.saveCity(city);
        return city;
    }

    @ResponseBody
    @GetMapping(value = "/city")
    public City getCityById(@RequestParam(value = "id") Long id) {
        return cityService.getCityById(id);
    }

    @GetMapping(value = "/account")
    @ResponseBody
    public Account getById(@RequestParam(value = "id") Long id) {
        return accountService.getAccountById(id);
    }

    @GetMapping(value = "/sql")
    @ResponseBody
    public String queryFromDb() {
        Long count = jdbcTemplate.queryForObject("select count(*) from account_tbl", Long.class);
        return count.toString();
    }

    /**
     * 来登录页
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage() {
        return "login";
    }

    /**
     * 方法的参数是一个自定义类型对象(从请求参数中确定的)，把他重新放在ModelAndViewContainer
     * @param user
     * @param session
     * @param model
     * @return
     */
    @PostMapping(value = "/login")
    public String main(User user, HttpSession session, Model model) { //RedirectAttributes
        if (StringUtils.hasLength(user.getUsername()) && "123456".equals(user.getPassword())) {
            //把登录成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登录成功重定向到main.html页面；重定向防止表单重复提交
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg","账号密码错误");
            //回到登录页面
            return "login";
        }

    }

    /**  不能直接访问templates文件夹下的html资源，必须经过请求处理才能访问，可以直接访问static文件夹下的资源
     * 去main页面
     * @return
     */
    @GetMapping(value = "/main.html")
    public String mainPage(HttpSession session,Model model) {
        // //是否登录。 拦截器，过滤器
        // Object loginUser = session.getAttribute("loginUser");
        // if (loginUser != null) {
        //     return "main";
        // } else {
        //     //回到登录页面
        //     model.addAttribute("msg","请重新登录");
        //     return "login";
        // }
        log.info("当前方法是：{}","mainPage");

        // ValueOperations<String, String> operationForValue = stringRedisTemplate.opsForValue();
        // String s = operationForValue.get("main.html");
        // String s1 = operationForValue.get("/sql");
        // model.addAttribute("mainCount",s);
        // model.addAttribute("sqlCount",s1);
        return "main";
    }
}
